package temporisateur;

import java.util.LinkedList;
import java.util.List;

public class TempoThread extends Thread{

	
	public boolean trig = false;
	public long time = 0;
	long timelimit = 0;
	LinkedList<TempoKeyListener> kls;
	
	public TempoThread(long timelimit)
	{
		kls = new LinkedList<TempoKeyListener>();
		this.timelimit = timelimit;
		start();
	}
	
	public void refresh(TempoKeyListener kl)
	{
		time = 0;
		trig = true;	
		if(!kls.contains(kl))
			kls.add(kl);
	}
	
	public void clear()
	{
		trig = false;
		TempoKeyListener.debutProc();
		for(TempoKeyListener kl : kls)
			kl.proc();
		kls.clear();
		TempoKeyListener.finProc();
	}
	public void run()
	{
		while(true)
		{
			
			try {
				sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			time += 50;
			if(trig && time>timelimit)
			{
				trig = false;
				TempoKeyListener.debutProc();
				for(TempoKeyListener kl : kls)
					kl.proc();
				kls.clear();	
				TempoKeyListener.finProc();
			}
		}
	}
}
